﻿#include "imgui.h"
#include "sdl2_opengl_base.h"

#include <vector>

class SDL2OpenGLBase04 : public SDL2OpenGLBase
{
private:
    std::vector<ImVec2> points;  // 存储所有点的位置
    int selected_point = -1;     // 选中的点的索引
    float point_radius = 5.0f;   // 点的半径

public:
    SDL2OpenGLBase04() {}
    ~SDL2OpenGLBase04() {}

    virtual int init() override
    {
        ImGuiIO& io = ImGui::GetIO(); (void)io;
        return 0;
    }

    virtual int render() override
    {
        ImGui::Begin("Main Window");

          // 获取标题栏高度
        float title_bar_height = ImGui::GetStyle().FramePadding.y * 2 + ImGui::GetFontSize();
        
        ImDrawList* draw_list = ImGui::GetWindowDrawList();
        ImVec2 window_pos = ImGui::GetWindowPos();
        ImVec2 mouse_pos = ImGui::GetMousePos();


        // 调整坐标计算，考虑内容区域的偏移
        ImVec2 content_offset = ImVec2(window_pos.x, window_pos.y + title_bar_height);

        // 左键点击添加新点
        if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) && 
            ImGui::IsWindowHovered())
        {
            ImVec2 local_pos = ImVec2(
                mouse_pos.x - content_offset.x,
                mouse_pos.y - content_offset.y
            );
            points.push_back(local_pos);
        }

        // 右键选中点
        if (ImGui::IsMouseClicked(ImGuiMouseButton_Right))
        {
            selected_point = -1;
            for (int i = 0; i < points.size(); i++)
            {
                ImVec2 abs_pos = ImVec2(
                    content_offset.x + points[i].x,
                    content_offset.y + points[i].y
                );
                float dist_sq = (mouse_pos.x - abs_pos.x) * (mouse_pos.x - abs_pos.x) +
                              (mouse_pos.y - abs_pos.y) * (mouse_pos.y - abs_pos.y);
                if (dist_sq <= point_radius * point_radius)
                {
                    selected_point = i;
                    break;
                }
            }
        }

        // 移动选中的点
        if (selected_point >= 0 && ImGui::IsMouseDown(ImGuiMouseButton_Right))
        {
            points[selected_point] = ImVec2(
                mouse_pos.x - content_offset.x,
                mouse_pos.y - content_offset.y
            );
        }

        // 绘制所有点
        for (int i = 0; i < points.size(); i++)
        {
            ImVec2 abs_pos = ImVec2(
                content_offset.x + points[i].x,
                content_offset.y + points[i].y
            );
            ImU32 color = (i == selected_point) ? 
                IM_COL32(0, 255, 0, 255) :  // 选中的点显示为绿色
                IM_COL32(255, 0, 0, 255);   // 未选中的点显示为红色
            draw_list->AddCircleFilled(abs_pos, point_radius, color);
        }

        // 显示点的数量和选中点的位置
        ImGui::Text("Points: %d", points.size());
        if (selected_point >= 0)
        {
            ImGui::Text("Selected Point: %d (%.1f, %.1f)", 
                selected_point, 
                points[selected_point].x, 
                points[selected_point].y);
        }

        ImGui::End();
        return 0;
    }
};

#undef main
int main(int argc, char* argv[])
{
    SDL2OpenGLBase04 app;
    app.run();

    return 0;
}